package org.jeecg.modules.parameter.biz.controlltr;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.parameter.api.dto.assistance.SimAssJsonDto;
import org.jeecg.modules.parameter.api.entity.assistance.SimAssPoints;
import org.jeecg.modules.parameter.api.entity.assistance.SimAssistance;
import org.jeecg.modules.parameter.api.vo.assistance.SimAssJsonVo;
import org.jeecg.modules.parameter.api.vo.assistance.SimAssPointsVo;
import org.jeecg.modules.parameter.biz.service.SimLineAssPointsService;
import org.jeecg.modules.parameter.biz.service.SimLineAssistanceService;
import org.jeecg.modules.parameter.common.api.dto.PatchPO;
import org.jeecg.modules.parameter.common.utils.oConvertUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lsy
 * @date 2024/10/05
 */
@Api(tags = "参数管理-线路辅助设施")
@RestController
@RequestMapping(value = {"/lineAssInfo"}, produces = {"application/json; charset=utf-8"})
@Slf4j
public class SimLineAssistanceController extends JeecgController<SimAssistance, SimLineAssistanceService> {
    @Resource
    private SimLineAssistanceService simLineAssistanceService;
    @Resource
    private SimLineAssPointsService simLineAssPointsService;

    @ApiOperation(value = "线路辅助设施-查询辅助设施", notes = "查询辅助设施")
    @PostMapping(value = "/getAssAndPoints")
    public Result<List<SimAssJsonVo>> getAssAndPoints() {
        return Result.ok(simLineAssistanceService.getAllAssJson());
    }

    @ApiOperation(value = "线路辅助设施-添加辅助设施", notes = "添加辅助设施")
    @PostMapping(value = "/addAssAndPoints")
    public Result<List<SimAssJsonVo>> addAssAndPoints(@RequestBody SimAssJsonDto simAssJsonDto) {
        SimAssistance simAssistance = oConvertUtils.entityToModel(simAssJsonDto, SimAssistance.class);
        List<SimAssPoints> simAssPointlist = oConvertUtils.entityListToModelList(simAssJsonDto.getPoints(), SimAssPoints.class);
        simLineAssistanceService.save(simAssistance);
        if (simAssPointlist != null) {
            for (SimAssPoints simAssPoints : simAssPointlist) {
                simLineAssPointsService.save(simAssPoints);
            }
        }
        return Result.ok("添加成功");
    }


    @ApiOperation(value = "线路辅助设施-修改线路辅助设施", notes = "修改线路辅助设施")
    @PutMapping(value = "/editAss")
    public Result<Boolean> alterAss(@RequestBody PatchPO patchPo) {
        return Result.ok(simLineAssistanceService.updatePatch(patchPo));
    }

    @ApiOperation(value = "线路辅助设施-删除辅助设施", notes = "删除线路")
    @DeleteMapping(value = "/deleteAss")
    public Result<String> deleteLine(@RequestParam(name = "id") String id) {
        simLineAssistanceService.removeById(id);
        simLineAssPointsService.delPoints(id);
        return Result.ok("删除成功");
    }

    @ApiOperation(value = "线路辅助设施-按线路ID查询线路辅助设施", notes = "按线路ID查询线路辅助设施")
    @PostMapping(value = "/getAssByLineId")
    public Result<List<SimAssJsonVo>> getAssByLineId(@RequestParam(name = "lineId", required = true) String id) {
        return Result.ok(simLineAssistanceService.getAllAssJsonByLineId(id));
    }

    @ApiOperation(value = "线路辅助设施-按ID查询", notes = "按ID查询")
    @PostMapping(value = "/getAssById")
    public Result<SimAssJsonVo> getAssById(@RequestParam(name = "id", required = true) String id) {
        SimAssJsonVo assPointsJson = oConvertUtils.entityToModel(simLineAssistanceService.getById(id), SimAssJsonVo.class);
        List<SimAssPointsVo> assPoints = oConvertUtils.entityListToModelList(simLineAssPointsService.getPoints(id), SimAssPointsVo.class);
        assert assPointsJson != null;
        assPointsJson.setPoints(assPoints);
        return Result.ok(assPointsJson);
    }
}
